/**
 * Created by wangguoqing on 2017/11/8.
 */
var role = parent.$('#tt').datagrid('getSelections')[0].roleDtos;
var params=new Object();
params.validMark = 1;//有效标志
var url=path+"/system/assignPermission/listRoleByPage";
var li = '<li id="li{0}" data-id="{0}"><a href="#" onclick="deleteRole(this);"><img src="<%=path%>/resource/images/system/deleteBtn.png" alt=""></a><div class="name">{1}</div></li>'

$(function(){
    //设置AjaxLoading
    setAjaxLoading();
    var grid=$("#tt");
    initGrid(grid);
    setUserRole();
});

//设置已选角色
function setUserRole(){
    var html = "";
    $.each(role,function(i,v){
        html += String.format(li, v.roleId, v.roleName);
    });
    $("#membersList").html(html);
}

function deleteRole(obj){
    $.each(role,function(i,v){
        if(v.roleId == $(obj).parent().attr("data-id")){
            role.splice(i,1); //删除
            return false;
        }
    });
    $(obj).parent().remove();
    var rows=$('#tt').datagrid('getRows');//获得行
    for(var i =0;i<rows.length;i++){
        if(rows[i].roleId == $(obj).parent().attr("data-id")){
            $('#tt').datagrid('unselectRow',i);//取消选择
        }
    }
}

//初始化表格和数据
function initGrid(grid) {
    var columns=[
        { field: "roleId", title: "ID", hidden: true, align: "left", sortable: false, resizable: true, width: 80},
        { field: "roleCode", title: "角色代码", align: "center",halign: "center", sortable: false, resizable: true, width: 80},
        { field: "roleName", title: "角色名称", align: "center",halign: "center", sortable: false, resizable: true, width: 140}
    ];

    initPageGridWithCheckBox(grid, params, url,columns,"#tb","roleId");
    var gridOptions = $("#tt").datagrid("options");
    gridOptions.onLoadSuccess=function(data){
        $('#tt').datagrid("fixRownumber");
        $.each(role,function(i,v){
            $.each(data.rows,function(index,val){
                if(v.roleId == val.roleId){
                    $("#tt").datagrid("selectRow",index);
                    setUserRole();
                }
            });
        });
    };
    gridOptions.onCheck=function(index, row){ //单击选中
        var f = true;
        $.each(role,function(i,v){
            if(v.roleId == row.roleId){
                f = false;
            }
        });
        if(f){
            role.push(row);
            setUserRole();
        }
    };
    gridOptions.onUncheck=function(index, row){ //单击取消选中
        $.each(role,function(i,v){
            if(v.roleId == row.roleId){
                role.splice(i,1); //删除
                return false;
            }
        });
        setUserRole();
    };
    gridOptions.onCheckAll=function(rows){ //单击全选
        $.each(rows,function(index,row){
            var f = true;
            $.each(role,function(i,v){
                if(v.roleId == row.roleId){
                    f = false;
                }
            });
            if(f){
                role.push(row);
            }
        });
        setUserRole();
    };
    gridOptions.onUncheckAll=function(rows){ //取消全选
        $.each(rows,function(index,row){
            $.each(role,function(i,v){
                if(v.roleId == row.roleId){
                    role.splice(i,1); //删除
                    return false;
                }
            });
        });
        setUserRole();
    };
    getPage(grid,params,url);



}


//查询按钮
function btnQuery(){
    params.roleCode = $("#roleCode").val();
    params.roleName = $("#roleName").val();
    var grid=$("#tt");
    getPage(grid,params,url);
}

function submit_Add(){
    var ids = "";
    $.each(role,function(i,value){
        ids+=value.roleId+",";
    });
    var param = new Object();
    param.roleIds = ids;
    var treeObj = parent.$.fn.zTree.getZTreeObj("tree");
    var nodes = treeObj.getSelectedNodes();
    param.orgCode = nodes[0].id;
    param.userId = parent.$('#tt').datagrid('getSelections')[0].userId;
    $.ajax({
        type:"POST",
        url:path+'/system/assignPermission/saveRoleByOrgPersonal',
        data:param,
        dataType:"json",
        async: false,
        success:function callback(data){
            var analyMsg = data;
            if(analyMsg.code!="0000"){
                //$.messager.alert('友情提醒',analyMsg.msg,'info',function(){});
                $.alert("default", "error", analyMsg.msg);
            }else{
                //$.messager.alert("友情提醒", "角色分配成功。", "info",function(){
                //    parent.btnQuery();
                //    setTimeout("closeLayer()",100);//设置延时，防止IE下出现异常
                //});
                $.alert("update", "info", "角色分配");
            }
        }
    });
}